package com.ourteam.product.requirement.api;

import com.ourteam.frame.web.AbstractTreeNodeBuilder;
import com.ourteam.product.requirement.dao.IProductFeatureDAO;
import com.ourteam.product.requirement.dao.ProductFeatureDAOQueryBean;
import com.ourteam.product.requirement.dao.ProductVersionRelateDAOFactory;
import com.ourteam.product.requirement.dao.ProductVersionRelateDAOQueryBean;
import com.ourteam.product.requirement.domain.ProductElementTypeEnum;
import com.ourteam.product.requirement.domain.ProductFeatureBean;
import com.ourteam.product.requirement.service.IProductService;
import com.ourteam.product.requirement.service.ProductServiceFactory;

public class ProductFeatureDefineSubNodeBuilder
		extends AbstractTreeNodeBuilder<ProductFeatureBean, ProductFeatureBean> {

	private static final IProductService PRODUCT_DEFINE_SERVICE = ProductServiceFactory.getProductService();

	private long productDefineId;

	private long productVersionId;

	@Override
	public ProductFeatureBean[] getTreeNodeObjects(ProductFeatureBean parentObject) throws Exception {

		long parentId = parentObject != null ? parentObject.getId() : 0;

		ProductFeatureDAOQueryBean queryBean = new ProductFeatureDAOQueryBean();

		queryBean.setSortProperty(IProductFeatureDAO.SortIndex);

		ProductFeatureDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		criteria.andProductDefineIdEqualTo(productDefineId).andParentIdEqualTo(parentId);

		if (productVersionId != 0) {
			ProductVersionRelateDAOQueryBean versionRelateQueryBean = new ProductVersionRelateDAOQueryBean();

			versionRelateQueryBean.addProductElementIdSelectProperty();

			versionRelateQueryBean.createCriteria().andProductVersionIdEqualTo(productVersionId)
					.andProductElementTypeEqualTo(ProductElementTypeEnum.PRODUCT_FEATURE.getName());

			criteria.andIdIn(versionRelateQueryBean, ProductVersionRelateDAOFactory.getProductVersionRelateDAO());
		}

		return PRODUCT_DEFINE_SERVICE.queryProductFeatures(queryBean);

	}

	@Override
	public Object getNodeId(ProductFeatureBean childObject) throws Exception {

		return childObject.getId();
	}

	@Override
	public boolean isLeaf(ProductFeatureBean childObject) throws Exception {
		return false;
	}

	@Override
	public int getChildCount(ProductFeatureBean childObject) throws Exception {
		long parentId = childObject != null ? childObject.getId() : 0;

		ProductFeatureDAOQueryBean queryBean = new ProductFeatureDAOQueryBean();

		ProductFeatureDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		criteria.andProductDefineIdEqualTo(productDefineId).andParentIdEqualTo(parentId);

		if (productVersionId != 0) {
			ProductVersionRelateDAOQueryBean versionRelateQueryBean = new ProductVersionRelateDAOQueryBean();

			versionRelateQueryBean.addProductElementIdSelectProperty();

			versionRelateQueryBean.createCriteria().andProductVersionIdEqualTo(productVersionId)
					.andProductElementTypeEqualTo(ProductElementTypeEnum.PRODUCT_FEATURE.getName());

			criteria.andIdIn(versionRelateQueryBean, ProductVersionRelateDAOFactory.getProductVersionRelateDAO());
		}

		return PRODUCT_DEFINE_SERVICE.queryProductFeatureCount(queryBean);
	}

	@Override
	public String getNodeDefineId(ProductFeatureBean childObject) throws Exception {
		return "productFeature";
	}

	@Override
	public ProductFeatureBean converToParentObject(Object parent) throws Exception {
		if (parent == null) {
			return null;
		} else {
			return PRODUCT_DEFINE_SERVICE.getProductFeatureById(Long.parseLong(parent.toString()));
		}

	}

	public void setProductDefineId(long productDefineId) {
		this.productDefineId = productDefineId;
	}

	public void setProductVersionId(long productVersionId) {
		this.productVersionId = productVersionId;
	}

}
